package com.gyc.simple.sysuser.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import com.gyc.simple.common.util.DateUtils;
import com.gyc.simple.common.util.MD5Utils;
import com.gyc.simple.common.util.UUIDUtils;
import com.gyc.simple.common.vo.ResponseType;
import com.gyc.simple.sysuser.config.AuthorityResources;
import com.gyc.simple.sysuser.config.ConfigConstant;
import com.gyc.simple.sysuser.entity.base.IUser;
import com.gyc.simple.sysuser.excel.excellistener.UserBatchListener;
import com.gyc.simple.sysuser.service.FileService;
import com.gyc.simple.sysuser.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.Map;
import java.util.Optional;

/**
 * @program: Inaction
 * @description:
 * @author: Mr.gyc
 * @create: 2019-08-29 20:37
 */
@RestController
@RequestMapping("/sys/user/")
public class IUserController {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private ConfigConstant constant;
    @Autowired
    private FileService fileService;

    @AuthorityResources(desc = "分页可带条件查询用户列表",url = "/sys/user/getUserList")
    @GetMapping("/getUserList")
    public String getUserList(@RequestParam Map paraMap) {
        try {
            return Optional.ofNullable(JSONObject.toJSONString(sysUserService.queryUserListByPageAndCondition(CommonMethod.checkPageParam(paraMap)))).orElse("");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    @AuthorityResources(desc = "修改用户",url = "/sys/user/putUser")
    @PutMapping("/putUser")
    public String putUser(@RequestBody IUser iUser) {
        ResponseType responseType = new ResponseType();
        try {
            Optional.ofNullable(iUser.getIuId()).ifPresent(vsl -> {
                int res = sysUserService.updateByPrimaryKeySelective(iUser);
                if (res > 0) {
                    responseType.setStatus(true);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONObject.toJSONString(responseType);
    }
    @AuthorityResources(desc = "新增用户",url = "/sys/user/addUser")
    @PostMapping("/addUser")
    public String addUser(@RequestBody IUser iUser) {
        ResponseType responseType = new ResponseType();
        try {
            Optional.ofNullable(iUser.getIuLname()).ifPresent(val -> {
                iUser.setIuId(UUIDUtils.getUUID32());
                Date date = new Date();
                iUser.setIuRegistertime(DateUtils.getTimestamp(date));
                iUser.setIuPassword("fctnh");
                String info = iUser.getIuPassword() + DateUtils.dateToString(date, "yyyy/MM/dd HH:mm:ss");
                iUser.setIuPassword(MD5Utils.getMD5String(info));
                int res = sysUserService.insertSelective(iUser);
                if (res > 0) {
                    responseType.setStatus(true);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONObject.toJSONString(responseType);
    }
    @AuthorityResources(desc = "判断用户是否存在",url = "/sys/user/ifPresentLname")
    @GetMapping("/ifPresentLname")
    public String ifPresentLname(@RequestParam String iuLname) {
        ResponseType responseType = new ResponseType();
        try {
            Optional.ofNullable(iuLname).ifPresent(val -> responseType.setStatus(sysUserService.ifPresentLname(iuLname)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONObject.toJSONString(responseType);
    }
    @AuthorityResources(desc = "删除用户",url = "/sys/user/deleteById")
    @DeleteMapping("/deleteById")
    public String deleteById(@RequestParam String iuId) {
        ResponseType responseType = new ResponseType();
        try {
            Optional.ofNullable(iuId).ifPresent(val -> {
                responseType.setStatus(sysUserService.deleteById(iuId));
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONObject.toJSONString(responseType);
    }
    @AuthorityResources(desc = "批量删除用户",url = "/sys/user/deleteAll")
    @DeleteMapping("/deleteAll")
    public String deleteAll(@RequestParam String ids) {
        ResponseType responseType = new ResponseType();
        try {
            Optional.ofNullable(ids).ifPresent(val -> {
                responseType.setStatus(sysUserService.deleteAllByIds(ids.split(","))>0);
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONObject.toJSONString(responseType);
    }
    @AuthorityResources(desc = "批量新增用户",url = "/sys/user/addBath")
    @PostMapping("/addBath")
    public String addBath(MultipartFile file){
        ResponseType responseType = new ResponseType();

        try{
            if(file!=null){
                UserBatchListener userBatchListener = new UserBatchListener(sysUserService);
                EasyExcel.read(file.getInputStream(), IUser.class,userBatchListener).sheet().doRead();
                if(userBatchListener.getDuplicateName().size()>0){
                    responseType.setData(userBatchListener.getDuplicateName());
                    responseType.setRespMsg("用户名已经存在！");
                }else{
                    responseType.setStatus(true);
                }
            }
            /*文件上传
            Optional.ofNullable(file).ifPresent(val->{
                EasyExcel.read(file.getInputStream(), UserBatchExcelData.class,new UserBatchListener()).sheet().doRead();
                responseType.setStatus(true);

                *//*String uploadDir = constant.getUploaddir()+"\\"+DateUtils.getLocalDateTimeToString(LocalDateTime.now(),"yyyyMMdd");
                String fileOldName = file.getOriginalFilename();
                String fileNewName = UUIDUtils.getUUID32();

                if(UploadUtils.saveFile(file,uploadDir,fileNewName,constant.getSuffix())){
                    IFile iFile = new IFile(fileNewName,fileOldName,"0000000001",
                            fileOldName.substring(fileOldName.lastIndexOf(".")+1),
                            "\\"+DateUtils.getLocalDateTimeToString(LocalDateTime.now(),"yyyyMMdd"),LocalDateTime.now());
                    if(fileService.insertSelective(iFile)){

                    }
                }*//*
            });*/
        }catch (Exception e){
            e.printStackTrace();
        }
        return JSONObject.toJSONString(responseType);
    }

}
